<?php
require_once('./modules/blog/blogpost.inc');
require_once('./modules/cache.inc');
require_once('./modules/util.inc');

class BlogTagRenderer extends Renderer
{
	protected $start;
	protected $ITEMS_IN_PAGE;
	protected $tag;
	protected $postItems;
	
	function pageLoad()
	{
		$this->ITEMS_IN_PAGE = $this->config->get('./maxItemsPerPage');
		$this->start = Util::safeGetPost('itemId');
		$this->start = !isset($this->start) || $this->start == "" ? "0" : $this->start;
		
		
		//loading it in page load function so all functions can enjoy it.
		$this->tag = new Tag();
		$this->tag->load(Util::safeGetPost('tagId'));
		$this->loadPostItems();
	
		switch( $this->action )
		{
			case "":
			case "show":
			default:
				break;
		}
	}

	function loadPostItems()
	{		
		//name the content type, with paging
		$cacheId = "blog-tag-".$this->tag->id."-latest-".$this->start;

		$cacheStatus = 'DEAD';
		
		$cache = new Cache();
		
		if( !$cache->ERROR )
			$cacheStatus = $cache->check_cache($cacheId);
		
		if($cacheStatus == 'HIT')
			$this->postItems = $cache->get($cacheId);
		else
		{
			$this->postItems = array
			(
				//we limit the fetch to the items that will be displayed on this page only,
				//so we need to retrieve the total count separately.				
				'items' => BlogPost::getTagPosts($this->tag->id,'creation_date desc',$this->start,$this->ITEMS_IN_PAGE),
				'count' => BlogPost::getTagCount($this->tag->id)
			);
			
			$cache->set($cacheId,$this->postItems);
		}	
	}
	
	function renderContent()
	{
		global $G_HOME;
		global $content;

		$paging = Util::getPaging($this->start,$this->postItems['count'],"pg=blog-tag&amp;tagId=".$this->tag->id);
		
		$content .= $paging;
		
		if( count($this->postItems['items']) > 0 )
		{
			$content .= "<h1>".$this->config->get('./siteName')." ".$this->tag->tag." tag</h1>";
		}
		else
			$content .= "<p/><h2>No more items</h2><p/>";
		
		foreach($this->postItems['items'] as $blogPost)
		{
			$postText = Util::cleanTags($blogPost->content);
					
			$content .= 
			"<div>
				<h2><a href='".$blogPost->getUrl()."'>".$blogPost->title."</a></h2>
				<h5>Date: ".date('d/m/Y',$blogPost->creationDate)."</h5>
				<h4>Comments: ".$blogPost->commentCount."</h4>
			</div>";

			$postText = substr($postText,0,min(strlen($postText),1000));
			if( strlen($postText) >3 && substr($postText,strlen($postText)-3,3) != '...')
				$postText .= "...";

			$content .= "<div style='line-height:1.5em;'>".$postText."</div><p/>";
			
		}
		$content .= $paging;
	}
	
	function renderBreadcrumbs()
	{
		parent::renderBreadcrumbs();
		
		global $breadcrumbs;
		
		$tagName = $this->tag->tag;
		
		$breadcrumbs .= "&nbsp;&#187;&nbsp;$tagName";
	}

	function setSEODetails()
	{
		global $title;
		global $description;
				
		$title = Util::cutStringFullWords($this->config->get('./siteName')." - ".$this->tag->tag." relatged blog entries, page ".$this->start,65).".";
		$description = Util::cutStringFullWords($this->config->get('./siteName')." - ".$this->tag->tag." - ".$this->tag->description." relatged blog entries, page ".$this->start,255).".";
	}
}

$renderer = new BlogTagRenderer();

?>